//
// Created by 123 on 2025/1/6.
//
#pragma once
#include "iostream"
using namespace std;

template<class T>
class MyArray{
private:
    //指向一个堆空间，这个空间存储真正的数据
    T* pAddress;
    //容量
    int capacity;
    //大小
    int size;
public:
    //构造函数
    MyArray(int capacity){
        this->capacity = capacity;
        this->size = 0;
        this->pAddress = new T[this->capacity];
    }
    //拷贝构造
    MyArray(const MyArray& arr){
        this->capacity = arr.capacity;
        this->size = arr.size;
        this->pAddress = new T[this->capacity];
        for (int i=0;i<this->size;i++){
            //如果T为对象，而且还包含指针，必须要重载=号，因为这个=号不是构造而是赋值
            //普通类型可以直接=，但是指针类型需要深拷贝
            this->pAddress[i] = arr[i];
        }
    }
    //重载=号，防止浅拷贝
    MyArray& operator =(const MyArray& myArray){
       if(this->pAddress != NULL){
            delete[] this->pAddress;
            this->capacity = 0;
            this->size = 0;
       }
       this->capacity = myArray.capacity;
       this->size = myArray.size;
       this->pAddress = new T[this->capacity];
       for(int i=0;i<this->size;i++){
           this->pAddress[i] = myArray[i];
       }
        return *this;
    }
    //重载[]
    T& operator [](int index){
        return this->pAddress[index];
    }
    //尾插法
    void pushBack(const T& val){
        if(this->capacity == this->size){
            return;
        }
        this->pAddress[this->size] = val;
        this->size++;
    }
    //尾删法
    void popBack(){
        if(this->size == 0){
            return;
        }
        this->size--;
    }
    //获取数组容量
    int getCapacity(){
        return this->capacity;
    }
    //获取数组大小
    int getSize(){
        return this->size;
    }
    //析构
    ~MyArray(){
        if(this->pAddress != NULL){
            delete[] this->pAddress;
            this->pAddress = NULL;
            this->capacity = 0;
            this->size = 0;
        }
    }

};